<?php
class Admin_link extends Link_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model("Link_m");
		$this->_data['sidebar'] = "link";
	}
	
	function index(){
		$this->list_partner_link();	
	}
	
	//--list categories--//
	function list_categories(){
		is_admin();
		$this->_data['page_title'] = "catégorie de la liste";
		$per_page = $this->uri->segment(3);		
		$off_set = $this->uri->segment(4);		
		if($per_page == ''){
			$per_page = 6;
		}
		if($off_set == ''){
			$off_set = '';
		}

		$config['base_url'] = base_url().'index.php/admin_link/list_categories/'.$per_page."/";
		$config['uri_segment'] = 4;
		$config['per_page'] = $per_page;
		$config['total_rows'] = $this->Link_m->count_categories();
		if($off_set > 0 && ($off_set == $config['total_rows'])){
			$off_set = $off_set-$per_page; 	
		}
		$this->_data['category_data'] = $this->Link_m->list_categories($per_page, $off_set);
		$this->pagination->initialize($config);
		$this->_data['pagination'] = $this->pagination->create_links();
    	$this->_data['total'] = $config['total_rows'];
    	$this->_data['perpage'] = $per_page;
    	$this->_data['offset'] = $off_set;
    	$this->display_admin('admin/link/list_categories');
	}
	
	//--add categorie--/
	function add_categories(){
		is_admin();
		$this->_data['page_title'] = "Ajouter catégorie";
		$config = array(
							array("field" => "name", "label" => "Nom de la catégorie",
								  "rules"=>"required|xss_clean|trim|callback_check_exist_category_name"
								  )
		
				       );
		$this->form_validation->set_rules($config);
		if($this->input->post("add_category")){
			if($this->form_validation->run() === FALSE){
				$this->display_admin("admin/link/add_category");		
			}else{
				$data = array("name" => $this->input->post("name"));
				if($this->Link_m->add_category($data)){
					$this->session->set_flashdata('error', lang('add') ." ". lang('success'));
    				redirect(site_url('admin_link/list_categories'));	
				}else{
					$this->_data['insert_error'] = "Insert failed!";
				}
				
			}
		}
		$this->display_admin("admin/link/add_category");
	}
	
	//--check exist category--//
	function check_exist_category_name($name){
		$data = $this->Link_m->count_categories_by_name($name);
		if($data>0){
			$this->form_validation->set_message("check_exist_category_name", "Nom de la catégorie existe déjà dans le système");
			return FALSE;
		}else{
			return TRUE;
		}
	}
	
	//--edit category--//
	function edit_category($id = null){
		is_admin();
		$this->_data['page_title'] = lang('edit_profile')." "."category";
		if($this->Link_m->get_category_details($id) === FALSE){
			redirect("admin_link/list_categories");
		}
		$category_data = $this->Link_m->get_category_details($id);
		$this->_data['category'] = $category_data;
		$this->_data['category_name'] = $category_data->name;
		$this->_data['category_id'] = $category_data->id;	
		$config = array(
							array("field" => "name", 
								  "label" => "Nom de la catégorie",
								  "rules" => "required|xss_clean|trim"
								 )
		
				       );
		$this->form_validation->set_rules($config);
		
		if($this->input->post('edit_category')){
			if($this->form_validation->run()){
				$data = array("name"=>$this->input->post("name"));
				if($this->Link_m->edit_category($id, $data)){
					$this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
					redirect("admin_link/list_categories");	
				}	
			}else{
				$this->display_admin('admin/link/edit_category');
			}			
		}else{
			
			$this->display_admin('admin/link/edit_category');
		}
		
			
	}
	
	//--delete category--//
	function delete_category($id){
		is_admin();
		$delete = $this->Link_m->delete_category($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect("admin_link/list_categories");
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect("admin_link/list_categories");
		}
	}
	
	//--list partner link--//
	function list_partner_link(){
		is_admin();
		$this->_data['page_title'] = "lien partenaire liste";
		$per_page = $this->uri->segment(3);		
		$off_set = $this->uri->segment(4);		
		if($per_page == ''){
			$per_page = 10;
		}
		if($off_set == ''){
			$off_set = '';
		}

		$config['base_url'] = base_url().'index.php/admin_link/list_partner_link/'.$per_page."/";
		$config['uri_segment'] = 4;
		$config['per_page'] = $per_page;
		$config['total_rows'] = $this->Link_m->count_partner_link();
		if($off_set > 0 && ($off_set == $config['total_rows'])){
			$off_set = $off_set-$per_page; 	
		}
		$this->_data['partner_data'] = $this->Link_m->list_partner_link($per_page, $off_set);
		$this->pagination->initialize($config);
		$this->_data['pagination'] = $this->pagination->create_links();
    	$this->_data['total'] = $config['total_rows'];
    	$this->_data['perpage'] = $per_page;
    	$this->_data['offset'] = $off_set;
    	$this->display_admin('admin/link/list_partner_link');
	}
	
	//--check exist link--//
	function check_exist_link($link, $kind){
		$parts = explode('.', $kind);
		$count = $this->Link_m->check_exist_link($link, $parts[0]);
		if($count > 0){
			$this->form_validation->set_message("check_exist_link", "Link existe déjà dans le système");
			return FALSE;
		}else{
			return TRUE;
		}
	}
	//--add partner link--/
	
	function add_partner_link(){
	 is_admin();
	 $this->_data['page_title'] = "Ajouter partner link";
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'link',
                'label' => 'Link',
                'rules' => 'required|callback_check_exist_link[partner.event]'
            )
           
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('add_partner_link')) {
            if ($this->form_validation->run()) {
              $image_path = './assets/link_img';
        	  $thumb_path = $image_path . '/thumbs/';
	    	  $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name' => true
         );
        
	    $this->load->library('upload', $config);
	        
	    if ( ! $this->upload->do_upload()) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'),
                        'kind' => "partner"
                        
                    );
                    $query = $this->Link_m->add_partner_link($data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('add') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('add') ." ". lang('fail'));
                    redirect('admin_link/list_partner_link');
                } else {
                  	$upload_info = $this->upload->data();
	              	$config = array(
		                        'source_image' => $upload_info['full_path'], //get original image
		                        'new_image' => $thumb_path,//save as new image //need to create thumbs first
		                        'maintain_ratio' => true,
		                        'width' => 300,
		                        'height' => 200
	                    		);
	                 
	                 $this->load->library('image_lib', $config); //load library
	                 $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'), 
                        'image' => $upload_info['file_name'],
                        'kind' => "partner"
                     
                    );
                    $query = $this->Link_m->add_partner_link($data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('add') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('add') ." ". lang('fail'));
                    redirect('admin_link/list_partner_link');
                }
            }
        }


        $this->display_admin('admin/link/add_partner_link');
	
	}
	
	//--update partner link
	function edit_partner_link($id=""){
        is_admin();
        $this->_data['page_title'] = lang("edit_profile"). " partner link"; 
        if($this->_data['query'] = $this->Link_m->get_details_partner_link($id) === FALSE){
            redirect(site_url('Admin_Link'));
        }
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'link',
                'label' => 'Link',
                'rules' => 'required'
            )
            
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('edit_partner_link')) {
            if ($this->form_validation->run()) {
                 $image_path = './assets/link_img';
        	  $thumb_path = $image_path . '/thumbs/';
	    	  $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name' => true
         		);
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                     $data = array(
                        'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'),
                        'kind' => "partner"
                        
                    	);
                    $query = $this->Link_m->update_partner_link($id, $data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('fail'));
                    redirect('admin_link');
                } else {
                    $query = $this->Link_m->get_details_partner_link($id);
                    if (!empty($query)) {
                        $info = $query;
                        if ($info->image != null) {
                            $image_to_delete = './assets/link_img/' . $info->image;
                            $thumb_to_delete = './assets/link_img/thumbs/' . $info->image;
                            delete_images($image_to_delete, $thumb_to_delete);
                        }
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'),
                        'kind' => "partner",
                    	'image'=> $upload_info['file_name']
                        
                    	);
                    $query = $this->Link_m->update_partner_link($id, $data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('fail'));
                    redirect('admin_link');
                }
            }
        }
        
        $this->_data['partner_link'] = $this->Link_m->get_details_partner_link($id);
       
        $this->display_admin('admin/link/edit_partner_link');
    }
    
	//--delete partner link--//
	function delete_partner_link($id){
		is_admin();
		$query = $this->Link_m->get_details_partner_link($id); 
        if (count($query)>=0) {
           $info = $query;
           if ($info->image != null) {
                   $image_to_delete = './assets/link_img/' . $info->image;
                   $thumb_to_delete = './assets/link_img/thumbs/' . $info->image;
                   delete_images($image_to_delete, $thumb_to_delete);
           }
        }
		$delete = $this->Link_m->delete_partner_link($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect('admin_link');
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect('admin_link');
		}
	}
	
	//--delete multi partner links--//
	function delete_multi_partner_links(){
		is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            $image = $this->Link_m->get_details_partner_link($adelete[$i]);
         
                $image_to_delete = './assets/link_img/' . $image->image;
                $thumb_to_delete = './assets/link_img/thumbs/' . $image->image;
                if ($image->image != null) {
                    delete_images($image_to_delete, $thumb_to_delete);
                }
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Link_m->delete_multi_partner_links($list);
        if ($query)
            $this->session->set_flashdata('error', lang('delete').' ' . lang('success'));
        else
            $this->session->set_flashdata('error', lang('delete').' ' . lang('fail'));
        redirect('admin_link');
	}
	
	//--list event schedule--//
	function event_schedule(){
		is_admin();
		$this->_data['page_title'] = "Event Schedule";
		$per_page = $this->uri->segment(3);		
		$off_set = $this->uri->segment(4);		
		if($per_page == ''){
			$per_page = 10;
		}
		if($off_set == ''){
			$off_set = '';
		}

		$config['base_url'] = base_url().'index.php/admin_link/event_schedule/'.$per_page."/";
		$config['uri_segment'] = 4;
		$config['per_page'] = $per_page;
		$config['total_rows'] = $this->Link_m->count_event_schedule();
		if($off_set > 0 && ($off_set == $config['total_rows'])){
			$off_set = $off_set-$per_page; 	
		}
		$this->_data['event_data'] = $this->Link_m->list_event_schedule($per_page, $off_set);
		$this->pagination->initialize($config);
		$this->_data['pagination'] = $this->pagination->create_links();
    	$this->_data['total'] = $config['total_rows'];
    	$this->_data['perpage'] = $per_page;
    	$this->_data['offset'] = $off_set;
    	$this->display_admin('admin/link/list_event_schedule');
	}
	
	//--add event schedule--/
	function add_event_schedule(){
	 is_admin();
	 $this->_data['page_title'] = "Ajouter event schedule";
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'link',
                'label' => 'Link',
                'rules' => 'required|callback_check_exist_link[event.partner]'
            ),
            array(
                'field' => 'category',
                'label' => 'Category',
                'rules' => 'required|xss_clean'
            )
           
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('add_event_schedule')) {
            if ($this->form_validation->run()) {
              $image_path = './assets/link_img';
        	  $thumb_path = $image_path . '/thumbs/';
	    	  $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name' => true
         );
        
	    $this->load->library('upload', $config);
	        
	    if ( ! $this->upload->do_upload()) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'),
                        'kind' => "event",
                        'cat_id'=>$this->input->post('category')
                        
                    );
                    $query = $this->Link_m->add_event_schedule($data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('add') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('add') ." ". lang('fail'));
                    redirect('admin_link/event_schedule');
                } else {
                  	$upload_info = $this->upload->data();
	              	$config = array(
		                        'source_image' => $upload_info['full_path'], //get original image
		                        'new_image' => $thumb_path,//save as new image //need to create thumbs first
		                        'maintain_ratio' => true,
		                        'width' => 300,
		                        'height' => 200
	                    		);
	                 
	                 $this->load->library('image_lib', $config); //load library
	                 $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'), 
                        'image' => $upload_info['file_name'],
                        'kind' => "event",
                    	'cat_id'=>$this->input->post('category')
                     
                    );
                    $query = $this->Link_m->add_event_schedule($data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('add') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('add') ." ". lang('fail'));
                    redirect('admin_link/event_schedule');
                }
            }
        }


        $this->display_admin('admin/link/add_event_schedule');
	
	}
	
	//--update event schedule--//
	function edit_event_schedule($id=""){
        is_admin();
        $this->_data['page_title'] = lang("edit_profile"). " event schedule"; 
        if($this->_data['query'] = $this->Link_m->get_details_event($id) === FALSE){
            redirect(site_url('Admin_link/event_schedule'));
        }
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'link',
                'label' => 'Link',
                'rules' => 'required'
            ),
            array(
                'field' => 'category',
                'label' => 'Category',
                'rules' => 'required|xss_clean'
            )
            
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('edit_event')) {
            if ($this->form_validation->run()) {
              $image_path = './assets/link_img';
        	  $thumb_path = $image_path . '/thumbs/';
	    	  $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000,
                    'encrypt_name' => true
         		);
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                     $data = array(
                       'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'),
                        'kind' => "event",
                        'cat_id'=>$this->input->post('category')
                        
                    	);
                    $query = $this->Link_m->update_event($id, $data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('fail'));
                    redirect(site_url('Admin_link/event_schedule'));
                } else {
                    $query = $this->Link_m->get_details_event($id);
                    if (!empty($query)) {
                        $info = $query;
                        if ($info->image != null) {
                            $image_to_delete = './assets/link_img/' . $info->image;
                            $thumb_to_delete = './assets/link_img/thumbs/' . $info->image;
                            delete_images($image_to_delete, $thumb_to_delete);
                        }
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                        'link' => $this->input->post('link'),
                        'kind' => "event",
                        'cat_id'=>$this->input->post('category'),
                    	'image'=> $upload_info['file_name']
                        
                    	);
                    $query = $this->Link_m->update_event($id, $data);
                    if ($query)
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
                    else
                        $this->session->set_flashdata('error', lang('edit_profile') ." ". lang('fail'));
                    redirect(site_url('Admin_link/event_schedule'));
                }
            }
        }
        
        $this->_data['event'] = $this->Link_m->get_details_event($id);
       
        $this->display_admin('admin/link/edit_event');
    }
    
	//--delete event schedule--//
	function delete_event_schedule($id){
		is_admin();
		$query = $this->Link_m->get_details_event($id); 
        if (count($query)>=0) {
           $info = $query;
           if ($info->image != null) {
                   $image_to_delete = './assets/link_img/' . $info->image;
                   $thumb_to_delete = './assets/link_img/thumbs/' . $info->image;
                   delete_images($image_to_delete, $thumb_to_delete);
           }
        }
		$delete = $this->Link_m->delete_event($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect(site_url('Admin_link/event_schedule'));
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect(site_url('Admin_link/event_schedule'));
		}
	}
	
	//--delete multi event schedule--//
	function delete_multi_event_schedule(){
		is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            $image = $this->Link_m->get_details_event($adelete[$i]);
         
                $image_to_delete = './assets/link_img/' . $image->image;
                $thumb_to_delete = './assets/link_img/thumbs/' . $image->image;
                if ($image->image != null) {
                    delete_images($image_to_delete, $thumb_to_delete);
                }
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Link_m->delete_multi_events($list);
        if ($query)
            $this->session->set_flashdata('error', lang('delete').' ' . lang('success'));
        else
            $this->session->set_flashdata('error', lang('delete').' ' . lang('fail'));
        redirect(site_url('Admin_link/event_schedule'));
	}
	
	//--add event schedulde description--//
	function add_event_schedule_description(){
		is_admin();
			
		if($this->input->post("add_event_description")){
			
				$data = array("description" => $this->input->post("event_description"));
				if($this->Link_m->add_event_description($data)){
					$this->session->set_flashdata('error', lang('add') ." ". lang('success'));
    				redirect(site_url('Admin_link/event_schedule'));	
				}else{
					$this->_data['insert_error'] = "Insert failed!";
					redirect(site_url('Admin_link/event_schedule'));
				}
				
			
		}
		
		if($this->input->post("edit_event_description")){
			$id = $this->Link_m->get_event_description()->id;
			
			$data = array("description" => $this->input->post("event_description"));
				if($this->Link_m->edit_event_description($id, $data)){
					$this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
    				redirect(site_url('Admin_link/event_schedule'));	
				}else{
					$this->_data['insert_error'] = "Insert failed!";
					redirect(site_url('Admin_link/event_schedule'));
				}
		}
	
		
	}
	
	

}